package com.lanqiao.controller;

import cn.hutool.json.JSONUtil;
import com.lanqiao.entity.TypeInfo;
import com.lanqiao.service.TypeService;
import com.lanqiao.service.impl.TypeServiceImpl;
import org.lanqiao.mvc.entity.YockMvcAnnotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@YockMvcAnnotation.Controller("/type")
public class TypeController {
    private TypeService typeService = new TypeServiceImpl();

    @YockMvcAnnotation.RequestMapping("/getAllType.do")
    @YockMvcAnnotation.ResponseBody
    public String getAllType(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        Map<String, Object> map = new HashMap<>();
        response.setContentType("application/json;charset=utf-8");  //告诉前端返回来的是一个json对象
        List<TypeInfo> typeInfos = typeService.queryAllType();  //获取是所有的书籍分类，包含父子分类
        map.put("code",200);
        map.put("data",typeInfos);
        String jsonStr = JSONUtil.toJsonStr(map);
        return jsonStr;
    }
}
